
define(['require', '../libs/DatePickerInline', './PageView','../libs/AjaxUtil','../libs/CommonTools'], function (require, DatePicker, PageView, AjaxUtil, Common) {
    return PageView.extend({
        type: 'DailyDetail',
        render:function () {
            PageView.prototype.render.apply(this,arguments);
            require('../../less/main.less')
            var $wrapper = $('<div class="main"></div>');
            this.$wrapper = $wrapper;
            this.$el.append($wrapper);
            return this;
        },
        setTopInfo:function (opt) {
            var _this = this;
            _this.$el.find('.name').html(opt.real_name);
            _this.$el.find('.number').html(opt.count+'份');
            _this.$el.find('.t_name').html(opt.t_name);
            _this.$el.find('.img-thumb').html('<img src="'+ opt.avatar +'">')

        },
        onResume:function(args){
            this.onRender(args);
        },
        onRender: function (_params) {
            var _this = this;
            require('../../less/dailydetail.less');
            var tmp = require('../templates/DailyDetail.html');
            var mid = _params.id;
            var t_id = _params.t_id;
            this.$wrapper.empty();
            this.$wrapper.append(tmp);
            new DatePicker({
                maxMonth:new Date(),
                timeFormat:'-',
                onCellClick:function (e) {
                    var $this = $(e.currentTarget);
                    if($this.hasClass('date-pikcer-disabled')) return;
                    $(e.currentTarget).addClass('active');
                    var count = $(e.currentTarget).data('count');
                    count = count ? count : 0;
                    _this.$el.find('.show-count').html('当天提交<span  class="number">'+ count +'份'+'</span>'+ _this.templateName +'日志');

                },
                showHasFlag:function ($el, array) {
                    _this.$el.find('.show-count').html('');
                    array.forEach(function (it, index) {
                        var year = it.getFullYear();
                        var month = it.getMonth() + 1;
                        var xhr = AjaxUtil.ajaxGetDeferred({
                            path:'/diary/getDayList',
                            params: {
                                year:year,
                                month:month,
                                mid:mid,
                                t_id:t_id
                            }
                        });
                        xhr.done(function (data) {
                            var list = data.list;
                            var opt = data.stat;
                            _this.setTopInfo(opt);
                            _this.templateName = opt.t_name;
                            if(list.lenght) return;
                            list.forEach(function (v, i) {
                                var $has = $el.find('.date-picker-current [data-date='+ v.p_date +']').addClass('has-flag');
                                if (v.item.length > 0) {
                                    var count = 0,
                                        name = '';

                                    v.item.forEach(function (val, key) {
                                        count += parseInt(val.count);
                                        name = val.t_name;
                                    });

                                    $has.data('count', count).data('tname', name);
                                } else if (v.item.length === 0 && v[0].t_id === t_id) {
                                    $has.data('count', v[0].count).data('tname', v[0].t_name);
                                }
                            });

                        }).fail(function (d) {
                            Common.Toast(d.msg);
                        });

                    });

                }
            });
            return this;
        }
    });
});